﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            window.onresize = resizeCanvas;
            document.addEventListener("visibilitychange", function () {
                    animation.pause();
                    if (animation.isPaused) {
                        playPauseButton.innerHTML = "&#xe102;"
                    }
            }, false);

            playPauseButton.onclick = function () {
                if (!animation.isEnabled) {
                    animation.start();
                    playPauseButton.innerHTML = "&#xe103;"
                }
                else if (animation.isPaused) {
                    animation.resume();
                    playPauseButton.innerHTML = "&#xe103;"
                }
                else {
                    animation.pause();
                    playPauseButton.innerHTML = "&#xe102;"
                }
            };

            stopButton.onclick = function () {
                animation.stop();
                playPauseButton.innerHTML = "&#xe102;"
            }

            args.setPromise(WinJS.UI.processAll().then(start));
            
        }
    };
    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };
    var animation;

    function start() {
        resizeCanvas();
        animation = new Loop.Animation(animationCanvas, window.Debug.debuggerEnabled);
        createPlanets(animation);
        animation.start();
    }

    function resizeCanvas() {
        animationCanvas.height = window.innerHeight;
        animationCanvas.width = window.innerWidth;
    }

function createPlanets(animation) {
    var sun = new Planets.Planet(animation);
    sun.radius = 40;
    animation.animationElements.push(sun);
    for (var i = 0; i < 1+rnd(8,2); i++) {
        var planet = new Planets.Planet(animation);
        planet.rotationCenter = [animationCanvas.width / 2, animationCanvas.height / 2];
        planet.radius = 10 + rnd(20);
        planet.speed = rnd(90, 1) * Math.PI * 2 / 360;
        planet.rotationRadius = sun.radius * 2 + i * 150 + rnd(100);
        planet.foreColor = "rgb(" + rnd(255) + "," + rnd(255) + "," + rnd(255) + ")";
        animation.animationElements.push(planet);

        for (var m = 0; m < rnd(5); m++) {
            var moon = new Planets.Moon(animation, planet);
            moon.radius = 5 + rnd(planet.radius / 3);
            moon.speed = rnd(180, 1) * Math.PI * 2 / 360 * (Math.random() > 0.5 ? -1 : 1);
            moon.rotationRadius = planet.radius + moon.radius + 10 + rnd(40);
            moon.foreColor = "rgb(" +
                (50 + rnd(205)) + "," +
                (50 + rnd(205)) + "," +
                (50 + rnd(205)) + ")";
            animation.animationElements.push(moon);
        }
    }
}

function rnd(max, min) {
    if (!min)
        min = 0;
    return Math.round(min+( Math.random() * (max == undefined ? 1 : (max-min))));
}

    app.start();
})();
